<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="deleteAll">全部删除</span>&nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon v-else
                name="delete"
                @click="isDeleteShow = true" />
    </van-cell>
    <van-cell :title="item"
              v-for="(item, index) in searchHistory"
              :key="index"
              @click="onSearchitemClick(item, index)">
      <van-icon name="close"
                v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  props: {
    searchHistory: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    deleteAll () {
      this.$dialog.confirm({
        title: '提示',
        message: '是否删除全部历史记录？'
      })
        .then(() => {
          this.$emit('clear-search-history')
        })
        .catch(() => {

        })
    },
    onSearchitemClick (item, index) {
      if (this.isDeleteShow) {
        this.searchHistory.splice(index, 1)
      } else {
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style>
</style>
